import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/rendering.dart';
import 'package:get/get.dart';
import 'package:part_time_job_pro/common/cache/app_box.dart';
import 'package:part_time_job_pro/common/langs/local.dart';
import 'package:part_time_job_pro/common/utils/system_preferred_util.dart';
import 'package:part_time_job_pro/common/utils/system_ui_style_util.dart';
import 'package:part_time_job_pro/common/values/images.dart';
import 'package:part_time_job_pro/pages/home/state.dart';
import 'package:part_time_job_pro/common/base/base.dart';
import 'package:part_time_job_pro/pages/service/logic.dart';

class HomeLogic extends BaseController with GetTickerProviderStateMixin {
  final HomeState state = HomeState();

  late AnimationController borderRadiusAnimationController;
  late AnimationController hideBottomBarAnimationController;
  late Animation<double> borderRadiusAnimation;
  late CurvedAnimation borderRadiusCurve;
  late ScrollController scrollController = ScrollController();

  final autoSizeGroup = AutoSizeGroup();

  //定时器
  @override
  void onInit() {
    super.onInit();
    SystemPreferredOrientationsUtil.setVertical();
    SystemUiOverlayStyleUtil().setWhiteSystemUiOverlayStyle();
    initAnimation();

    // 监听语言变化
    Get.find<ServiceLogic>().state.languageIndex.listen((value) {
      debugPrint("语言切换======$value");
      update();
    });
  }

  @override
  void onClose() {
    borderRadiusAnimationController.dispose();
    hideBottomBarAnimationController.dispose();
    scrollController.dispose();
    super.onClose();
  }

  String getTranslatedLabel(int index) {
    switch (index) {
      case 0:
        return Local.offer.tr;
      case 1:
        return Local.find.tr;
      case 2:
        return Local.message.tr;
      case 3:
        return Local.mine.tr;
      default:
        return '';
    }
  }

  //改变选中
  void changSelectedIndex(int index) {
    state.currentIndex = index;
    update();
  }

  //初始化动画
  void initAnimation() {
    scrollController.addListener(() {
      if (scrollController.offset != 0) {
        state.scrollBarThickness = 2.0;
      } else {
        state.scrollBarThickness = 0.0;
      }
      update();
    });

    borderRadiusAnimationController = AnimationController(duration: const Duration(milliseconds: 500), vsync: this);

    borderRadiusCurve = CurvedAnimation(
      parent: borderRadiusAnimationController,
      curve: const Interval(0.5, 1.0, curve: Curves.fastOutSlowIn),
    );

    borderRadiusAnimation = Tween<double>(begin: 0, end: 1).animate(borderRadiusCurve);

    hideBottomBarAnimationController = AnimationController(duration: const Duration(milliseconds: 200), vsync: this);

    //顶部发布按钮
    borderRadiusAnimationController.forward();
  }

  //滚动通知
  bool onScrollNotification(ScrollNotification notification) {
    if (notification is UserScrollNotification && notification.metrics.axis == Axis.vertical) {
      switch (notification.direction) {
        case ScrollDirection.forward:
          hideBottomBarAnimationController.reverse();
          break;
        case ScrollDirection.reverse:
          hideBottomBarAnimationController.forward();
          break;
        case ScrollDirection.idle:
          break;
      }
    }
    return false;
  }

  ///更新当前下标
  void updateCurrentIndex(index) {
    state.currentIndex = index;
    update();
  }

  void updateModalData() {
    //模拟更新
    state.publicTypeList = [
      {"id": 0, "icon": AssetsImages.jzIcon, "name": "发布兼职", "count": 8},
      {"id": 1, "icon": AssetsImages.qzIcon, "name": "发布全职", "count": 9},
      {"id": 2, "icon": AssetsImages.xzIcon, "name": "发布校招", "count": 10},
    ];
    state.ishowModal = true;
    update(["bottomPubSheet"]);
  }
}
